//app.js
App({
  globalData: {
    cartsList: wx.getStorageSync('cartslist') || []
  },

  saveToStorage: function() {
    wx.setStorageSync('cartslist', this.globalData.cartsList)
  },

  addCartsList: function(goodsInfo) {
    var flag = this.globalData.cartsList.findIndex(item => item.id === goodsInfo.goods_id)

    if(flag !== -1) {
      this.globalData.cartsList[flag].count += 1
      this.saveToStorage()
      return 
    }

    var goods = {
      id: goodsInfo.goods_id, 
      count: 1, 
      isChecked: true,
      src: goodsInfo.goods_big_logo, 
      title: goodsInfo.goods_name, 
      price: goodsInfo.goods_price
    }

    this.globalData.cartsList.unshift(goods)
    this.saveToStorage()
  },

  delCartsList: function() {
    this.saveToStorage()
  },

  checkCartsList: function(id, status) {
    var index = this.globalData.cartsList.findIndex(item => item.id === id)

    this.globalData.cartsList[index].isChecked = status

    this.saveToStorage()
  },

  changeCartsList: function(id, count) {
    var index = this.globalData.cartsList.findIndex(item => item.id === id)

    this.globalData.cartsList[index].count = count

    this.saveToStorage()
  }
})